define(["drawable"], function(Drawable){
 
    var colliderCollisionListener = function(coll){

        /*
        var SIDE_NONE = -1;
        var SIDE_TOP = 0;
        var SIDE_RIGHT = 1;
        var SIDE_BOTTOM = 2;
        var SIDE_LEFT = 3;
          */
        
        var collider = coll;
        var collidables = [];
        
        this.registerCollidable = function(collidable){
            collidables.push(collidable);
        };

        this.registerCollidableArray = function(collidableArray){
            for(var i=0; i<collidableArray.length; ++i){
                collidables.push(collidableArray[i]);
            }
        };

        this.reset = function(){
            collidables.length = 0;
        };

                  
        this.listen = function(){
            var collidable = null;
                        
            // collision side only for collider
            collider.collisionSide = collider.SIDE_NONE;
                        
            for(var i=0; i< collidables.length; ++i){
                collidable = collidables[i];
                
                if(!collidable.isCollidable)
                {
                    continue;
                }
    
                // bbox intersection test        
                if( Math.abs(collider.getCenterX() - collidable.getCenterX()) < (collider.w + collidable.w)/2 &&
                    Math.abs(collider.getCenterY() - collidable.getCenterY()) < (collider.h + collidable.h)/2 ){

                    // determine which sides collided
                    
                    var colliderTop = collider.y;
                    var colliderRight = collider.x + collider.w;
                    var colliderBottom = collider.y + collider.h;
                    var colliderLeft = collider.x;
                    
                    var collidableTop = collidable.y;
                    var collidableRight = collidable.x + collidable.w;
                    var collidableBottom = collidable.y + collidable.h;
                    var collidableLeft = collidable.x;
                    
                    if(colliderLeft < collidableLeft && 
                        collidableLeft < colliderRight && 
                        colliderRight < collidableRight){   
                        collider.collisionSide = collider.SIDE_LEFT;
                    }
    
                    else if(collidableLeft < colliderLeft && 
                        colliderLeft < collidableRight && 
                        collidableRight < colliderRight){
                        collider.collisionSide = collider.SIDE_RIGHT;
                    }
    
                    else if(colliderTop < collidableTop && 
                        collidableTop < colliderBottom && 
                        colliderBottom < collidableBottom){
                        collider.collisionSide = collider.SIDE_BOTTOM;
                    }
    
                    else if(collidableTop < colliderTop && 
                        colliderTop < collidableBottom && 
                        collidableBottom < colliderBottom){
                        collider.collisionSide = collider.SIDE_TOP;
                    }
                
                    if(collider.collisionSide != collider.SIDE_NONE){                    
                        collider.collides(collidable);
                        collidable.collides(collider);             
                    }
                    break; // collision done
                }            
            }
        };
    };
    
    return colliderCollisionListener;
});